﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace HyperPin_Database_Editor
{
    public partial class NewTableDialog : Form
    {
        public string TableName
        {
            get { return textTableName.Text; }
        }

        public string TableDescription
        {
            get { return textDescription.Text; }
        }

        public string TableManufacturer
        {
            get { return textManufacturer.Text; }
        }

        public string TableYear
        {
            get { return textYear.Text; }
        }

        public string TableType
        {
            get { return textType.Text; }
        }

        public NewTableDialog()
        {
            InitializeComponent();
        }

        private void textBoxXMLData_TextChanged(object sender, EventArgs e)
        {
            buttonOK.Enabled = ParseXML();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (ParseXML() == false)
            {
                DialogResult result = MessageBox.Show("XML data does not parse to a valid table description.", "Invalid XML");
                return;
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private bool ParseXML()
        {
            textTableName.Text = null;
            textDescription.Text = null;
            textManufacturer.Text = null;
            textYear.Text = null;
            textType.Text = null;

            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                // Load the xml
                xmlDoc.LoadXml(textBoxXMLData.Text);
                XmlNode tableNode = xmlDoc.SelectSingleNode(TableSummary.xmlTable);
                if (tableNode != null)
                {
                    XmlAttributeCollection tableAttributes = tableNode.Attributes;
                    XmlNode tableNameNode = tableAttributes.GetNamedItem(TableSummary.xmlName);

                    textTableName.Text = tableNameNode.InnerText;

                    if (tableNode.SelectSingleNode(TableSummary.xmlDescription) != null)
                    {
                        textDescription.Text = tableNode.SelectSingleNode(TableSummary.xmlDescription).InnerText;
                    }

                    if (tableNode.SelectSingleNode(TableSummary.xmlManufacturer) != null)
                    {
                        textManufacturer.Text = tableNode.SelectSingleNode(TableSummary.xmlManufacturer).InnerText;
                    }

                    if (tableNode.SelectSingleNode(TableSummary.xmlYear) != null)
                    {
                        textYear.Text = tableNode.SelectSingleNode(TableSummary.xmlYear).InnerText;
                    }

                    if (tableNode.SelectSingleNode(TableSummary.xmlType) != null)
                    {
                        textType.Text = tableNode.SelectSingleNode(TableSummary.xmlType).InnerText;
                    }

                    return true;
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return false;
        }
    }
}
